package com.l.web.service.system.group;

import org.l.qiao.Cluster;
import org.l.qiao.ClusterUtil;
import org.l.qiao.Validate;
import org.l.qiao.search.Query;
import com.l.web.service.ServiceSupport;
import com.l.web.service.system.user.User;
import com.l.web.service.system.user.UserDAO;
import org.apache.log4j.Logger;

import java.util.List;

public class GroupDetailService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(GroupDetailService.class);
    private GroupDAO groupdao;
    private UserDAO userdao;
    private Group group = new Group();
    private String id = "";
    private List<Cluster> users;

    public void setGroupdao(GroupDAO groupdao) {
        this.groupdao = groupdao;
    }

    public void setUserdao(UserDAO userdao) {
        this.userdao = userdao;
    }

    public String execute() {
        id = form.getString("id");
        LOGGER.debug("查看组信息：id=" + id);
        if (Validate.check(Validate.OBJECTID, id)) {
            group.putAll(groupdao.findById(id));
            if (group.isEmpty()) {
                LOGGER.error("所查看的用户组不存在");
                error(404);
                return NONE;
            }
            Query query = new Query();
            query.eq(User.GROUP_ID, id);
            users = userdao.find(query, null, null);
            putData();
            return "detail";
        } else {
            error(404);
            LOGGER.error("非法查看组信息：id=" + id);
            return NONE;
        }
    }

    private void putData() {
        context.put("group", group);
        context.put("check", ClusterUtil.getCheck(Group.class));
        context.put("users", users);
    }

}
